package com.open.capacity.points.controller;

import cn.hutool.core.date.DateUtil;
import com.open.capacity.common.constant.PointsRuleConstant;
import com.open.capacity.common.exception.controller.ControllerException;
import com.open.capacity.common.web.Result;
import com.open.capacity.points.constant.BusinessConstant;
import com.open.capacity.points.feign.UserFeignClient;
import com.open.capacity.points.service.PointsLogService;
import com.open.capacity.points.service.PointsRuleService;
import com.open.capacity.points.service.PointsRuleTypeService;
import com.open.capacity.points.service.PointsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/pointsBusiness")
public class PointsBusinessController {
    @Autowired
    PointsService pointsService;
    @Autowired
    PointsLogService pointsLogService;
    @Autowired
    PointsRuleService pointsRuleService;
    @Autowired
    PointsRuleTypeService pointsRuleTypeService;
    @Autowired
    UserFeignClient userFeignClient;

    /**
     * 每日签到积分逻辑处理
     *
     * @return
     */
    @PostMapping("signIn/{userId}")
    public Result signIn(@PathVariable Long userId) throws ControllerException {
        Map<String, Object> cond = new HashMap<>();
        cond.put("userid", userId);
        cond.put("prcode", PointsRuleConstant.SIGNIN);
        cond.put("startDate", DateUtil.today());
        Long count = pointsLogService.findPointsLogByCondition(cond).getCount();
        if (count > 0) return Result.failed(BusinessConstant.SignIn.SIGNED_IN_TODAY);
        Result result = pointsService.change(userId, PointsRuleConstant.SIGNIN, 0);
        if (result.getCode() == 0) {
            return Result.succeed(BusinessConstant.SignIn.SIGN_IN_SUCCEED.concat(result.getData().toString()));
        }
        return Result.failed(BusinessConstant.SignIn.SIGN_IN_FAILED);
    }
}
